package com.example.accessibilityhelper.common.backtomain

import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityEvent
import com.example.accessibilityhelper.Step
import com.example.accessibilityhelper.WeChatHelperService
import com.example.accessibilityhelper.config.Page
import com.example.accessibilityhelper.util.App
import com.example.accessibilityhelper.util.isWindowStateChanged

class GotoMainStep: RewindStep() {

    var done = false

    override fun onStart(){
        App.service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
    }

    override fun thisMatch(service: AccessibilityService, event: AccessibilityEvent): Boolean {
        return done.not()
    }

    override fun executeOnce(service: AccessibilityService, event: AccessibilityEvent) {
        if (event.isWindowStateChanged() && event.className!=WeChatHelperService.PAGE.MAIN_PAGE)
            service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
        else{
            done = true
            finish()
        }
    }

}